﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Admin
{
    public partial class newsupdate : System.Web.UI.Page
    {
        private int NewsID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                 NewsID = Int32.Parse(Request.QueryString["id"]);
            }catch{
                //有异常直接跳转到NewsList页面
                Response.Redirect("~/newslist.aspx");
            }
            if(!IsPostBack){
                BindData();
                InitData(NewsID);
            }
        }
        private void BindData()
        {
            BindTopFlag();
            BindNewsCatogory();
        }
        /// <summary>
        /// 读取出一个newsinfo，然后进行填充值
        /// </summary>
        private void InitData(int NewsID)
        {
            NewsInfo news = NewsInfoLogic.GetOneNewsInfo(NewsID);
            if(news == null){
                //参数无效
                //此处跳转到News List页面
                return;
            }
            txbTitle.Text                  = news.Title;
            ddlTop.SelectedValue    = news.TopFlag.ToString();
            newsContent.Text = news.NewsContent;
            ckbHot.Checked = news.HotFlag == 1 ? true : false;
            ddlCategory.SelectedValue = news.CategoryID.ToString();
            ImageShow.ImageUrl = news.NewsPicURL;
        }
        /// <summary>
        /// 绑定TopFlag 从0到100; 
        /// 0为不置顶,其余从100-1倒序
        /// </summary>
        private void BindTopFlag()
        {
            List<int> nddlTop = new List<int>();
            for (int i = 0; i <= 100; i++)
                nddlTop.Add(i);
            ddlTop.DataSource = nddlTop;
            ddlTop.DataBind();
        }

        private void BindNewsCatogory()
        {
            List<NewsCategoryInfo> list = new List<NewsCategoryInfo>();
            list = NewsCategoryInfoLogic.GetAllNewsCategoryInfo();
            if (list == null)
            {
                list.Add(new NewsCategoryInfo());
            }
            ddlCategory.DataSource = list;
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            NewsInfo news = NewsInfoLogic.GetOneNewsInfo(NewsID);
            if(news == null){
                return;
            }
            news.Title = txbTitle.Text;
            news.HotFlag = ckbHot.Checked ? 1 : 0;
            news.NewsContent = newsContent.Text;
            string strImgUrl = FileImgUpload();
            if (strImgUrl != "")
                news.NewsPicURL = strImgUrl;
            //news.NewsPicURL = ??//如果修改图片应该如何，如果不修改应该如何？
            try
            {
                news.TopFlag = Int32.Parse(ddlTop.SelectedValue);
                news.CategoryID = Int32.Parse(ddlCategory.SelectedValue);
            }
            catch{ }

            NewsInfoLogic.UpdateNewsInfo(news);            
        }
        private string FileImgUpload()
        {
            string uploadName = fileImg.FileName;//获取待上传图片的完整路径，包括文件名 
            if(uploadName == "" || uploadName == null){
                return "";
            }
            //string uploadName = InputFile.PostedFile.FileName; 
            string pictureName = "";//上传后的图片名，以当前时间为文件名，确保文件名没有重复 
            if (fileImg.FileName != "")
            {
                int idx = uploadName.LastIndexOf(".");
                string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名 
                pictureName = DateTime.Now.Ticks.ToString() + suffix;
            }
            try
            {
                if (uploadName != "")
                {
                    string path = Server.MapPath("~/uploads/");
                    fileImg.PostedFile.SaveAs(path + pictureName);
                    return "/uploads/" + pictureName;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(ex);
            }
            return "";
        }
    }
}
